package com.kexun;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kexun.bo.PlayUrlBO;
import com.kexun.bo.SongBO;
import com.kexun.platform.migu.Entrance;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;

public class MainApp {

    public static void main(String[] args) throws SQLException {
        Entrance entrance = new Entrance();//1000001635
//        saveGdListByTagId(entrance, "1000001635");
//        saveByKeyword(entrance, "https://music.migu.cn/v3/search?page=1&type=song&i=f2bd465a20a3d68645af277ec0542ba582f00e77&f=html&s=1649775745&c=001002A&keyword=%E7%8E%8B%E7%90%AA&v=3.22.7");
        saveByGdID(entrance, "188370032");
        exit();
    }

    private static void saveByGdID(Entrance entrance, String gdId) {
        List<SongBO> gdSong = entrance.getGdSong(gdId);
        saveSongList(gdSong);
    }

    private static void saveByKeyword(Entrance entrance, String url) {
        List<SongBO> songBOList = entrance.getKeyWordSong(url);
        saveSongList(songBOList);
    }

    private static void saveGdListByTagId(Entrance entrance, String tagId) {
        JSONArray list = entrance.getGdListByTagID(tagId);
        for (Object item : list) {
            try {
                JSONObject gdItem = (JSONObject) item;
                String gdId = gdItem.getString("gdId");
                String gdName = gdItem.getString("gdName");
                List<SongBO> gdSong = entrance.getGdSong(gdId);
                System.out.println("下单歌单[" + gdName + "] 共" + gdSong.size() + "首音乐");
                saveSongList(gdSong);
            } catch (Exception e) {
                System.err.println("下载错误:" + e.getMessage());
            }
        }
    }


    private static void exit() {
        System.out.println("下载完成");
        while (true) {
            System.out.println("输入 exit 退出");
            Scanner scanner = new Scanner(System.in);
            String next = scanner.next();
            if ("exit".equals(next)) {
                System.out.println("退出");
                break;
            }
        }
    }


    private static void saveSongList(List<SongBO> list) {
        CompletableFuture.runAsync(() -> {
            for (SongBO songBO : list) {
                try {
                    List<PlayUrlBO> playUrls = songBO.getPlayUrls();
                    if (playUrls.size() > 0) {
                        String songName = songBO.getSongName();
                        PlayUrlBO playUrlBO = playUrls.get(playUrls.size() - 1);
                        String url = playUrlBO.getUrl();
                        String lrcText = songBO.getLrcText();
                        String singer = songBO.getSinger();
                        String songImg = songBO.getSongImg();
                        Number number = Db.use().queryNumber("SELECT COUNT(0) FROM music WHERE music=?", songName);
                        int count = number.intValue();
                        if (count <= 0) {
                            Db.use().insert(
                                    Entity.create("music")
                                            .set("music", songName)
                                            .set("singer", singer)
                                            .set("src", url)
                                            .set("lrc", lrcText)
                                            .set("img", songImg)
                                            .set("create_time", new Date())
                            );
                            System.out.println("保存成功[" + songBO.getSongName() + "]");
                        }
                    }
                } catch (Exception e) {
                    System.err.println("下载错误:" + e.getMessage());
                }
            }
        });
    }
}
